#ifndef AUDIODATA_H
#define AUDIODATA_H

#include <QObject>
//#include <QAudioDecoder>
//#include <QAudioBuffer>
#include <QDebug>
//#include <QAudioDecoderControl>
#include <QFile>
#include "libzplay.h"
#include <QTimer>

#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
using namespace libZPlay;

class AudioData : public QObject
{
    Q_OBJECT
public:
    explicit AudioData(QObject *parent = 0);

    Q_INVOKABLE void setSource(QString source);
    Q_INVOKABLE void playMusic();
    Q_INVOKABLE void stopMusic();
    Q_INVOKABLE void pauseMusic();
    Q_INVOKABLE void seekPosition(int ms);
    Q_PROPERTY(int amplitude READ amplitude NOTIFY amplitudeChanged)
    Q_PROPERTY(int position READ position NOTIFY positionChanged)
    Q_PROPERTY(bool isPlaying READ isPlaying NOTIFY isPlayingChanged)

    //music pcm data
    Q_PROPERTY(QString pcmData READ pcmData NOTIFY pcmDataChanged)

    QString pcmData(){
        QJsonObject root;
        QJsonArray hn;
        for(int i =0;i<257;i++){
            hn.append (pnLeftAmplitude[i]);
        }
        root.insert ("data",hn);
        QJsonDocument doc;
        doc.setObject (root);
        return doc.toJson ();
    }

signals:
    void amplitudeChanged();
    void positionChanged();
    void isPlayingChanged();
    void pcmDataChanged();

public slots:
    void refreshDatas();
private:
    ZPlay *player;
    QTimer timer;
    int  pnHarmonicNumber[512];
    int  pnHarmonicFreq[257];
    int  pnLeftAmplitude[257];
    int  pnRightAmplitude[257];
    int  pnLeftPhase[257];
    int  pnRightPhase[257];
    int mposition=0;
    bool isMusicPlaying;

    int amplitude(){
        return pnRightAmplitude[171];
    }

    int position(){
        return mposition;
    }

    bool isPlaying(){
        return isMusicPlaying;
    }
};
#endif // AUDIODATA_H
